﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;

namespace FlashGames
{
  public class FlashGames
  {
    ConfigUtility configUtility = new ConfigUtility();
    public Site SelectedSite { get; set; }
    public Category SelectedCategory { get; set; }
    public Game SelectedGame { get; set; }
    public static string BaseUrl { get; set; }
    public string CurrentUrl { get; set; }

    public string GameFolderPath
    {
      get { return configUtility.GetGamesFolder(); }
      set { configUtility.SaveFolderPath(value); }
    }


    public FlashGames()
    {

    }

    public virtual void SaveSite(Site site)
    {
      configUtility.SaveSite(site);
    }

    public virtual void DeleteSite(Site site)
    {
      configUtility.RemoveSite(site.Name);
    }

    public virtual List<Site> GetSiteList()
    {
      List<Site> Sites = new List<Site>(configUtility.ReadSiteXml());
      return Sites;
    }

    public virtual List<Game> GetMyGames()
    {
      if (!string.IsNullOrEmpty(GameFolderPath))
      {
        List<Game> Games = new List<Game>();

        string[] files = Directory.GetFiles(GameFolderPath);
        foreach (string file in files)
        {
          if (file.Contains("swf") || file.Contains("unity3d"))
          {
            string name = file.Substring(GameFolderPath.Length, file.IndexOf(".") - GameFolderPath.Length).Replace(@"\", "");
            Game game = new Game() { FileUrl = file, Name = name };
            Games.Add(game);
          }
        }
        return Games;
      }
      return null;
    }

    public virtual string PlainTextfromHtml(string input)
    {
      return Util.PlainTextFromHtml(input);
    }

    public virtual Site LoadSite(Site site)
    {
      SelectedSite = site;
      BaseUrl = site.Url;
      Util.baseUrl = site.Url;
      if (!site.CategoryDiscovered)
        site.LoadCategories();
      return site;
    }

    public virtual string ResoveUrl(string Url)
    {
      return Util.ResolvedUrl(Url, BaseUrl);
    }

    public virtual List<Object> GetLast(ref Object ListItem)
    {
      List<Object> ItemList = new List<Object>();
      if (ListItem == null)
        return null;
      else if (ListItem.GetType() == typeof(Site))
      {
        ItemList.AddRange(GetSiteList());
        ListItem = null;
      }
      else if (ListItem.GetType() == typeof(Category))
      {
        SelectedCategory = (Category)ListItem;
        if (SelectedCategory.ParentCategory != null)
        {
          if (SelectedCategory.ParentCategory.SubCategoryDiscovered)
            ItemList.AddRange(SelectedCategory.ParentCategory.SubCategories);
          else ItemList.AddRange(SelectedCategory.ParentCategory.LoadCategories());
          ListItem = SelectedCategory.ParentCategory;
        }
        else if (SelectedCategory.ParentSite != null)
        {
          if (SelectedCategory.ParentSite.CategoryDiscovered)
            ItemList.AddRange(SelectedCategory.ParentSite.Categories);
          else ItemList.AddRange(SelectedCategory.ParentSite.LoadCategories());
          ListItem = SelectedCategory.ParentSite;
        }
      }
      else if (ListItem.GetType() == typeof(Game))
      {
        SelectedGame = (Game)ListItem;
        if (SelectedGame.ParentCategory != null)
        {
          if (SelectedGame.ParentCategory.ParentCategory != null && SelectedGame.ParentCategory.ParentCategory.SubCategoryDiscovered)
          {
            ItemList.AddRange(SelectedGame.ParentCategory.ParentCategory.SubCategories);
            ListItem = SelectedGame.ParentCategory;
          }
          else if (SelectedGame.ParentCategory.ParentSite != null && SelectedGame.ParentCategory.ParentSite.CategoryDiscovered)
          {
            ItemList.AddRange(SelectedGame.ParentCategory.ParentSite.Categories);
            ListItem = SelectedGame.ParentSite;
          }
        }
        else
        {
          ItemList.AddRange(GetSiteList());
          ListItem = null;
        }
      }
      return ItemList;
    }

    public virtual List<Object> GetNext(Object ListItem)
    {
      List<Object> ItemList = new List<Object>();
      if (ListItem.GetType() == typeof(Site))
      {
        SelectedSite = (Site)ListItem;
        CurrentUrl = SelectedSite.Url;
        if (SelectedSite.HasCategories)
        {
          if (SelectedSite.CategoryDiscovered)
            ItemList.AddRange(SelectedSite.Categories);
          else ItemList.AddRange(SelectedSite.LoadCategories());
        }
        else if (SelectedSite.HasGames)
        {
          if (SelectedSite.GamesDiscovered)
            ItemList.AddRange(SelectedSite.Games);
          else ItemList.AddRange(SelectedSite.LoadGames());
        }
      }
      else if (ListItem.GetType() == typeof(Category))
      {
        SelectedCategory = (Category)ListItem;
        CurrentUrl = SelectedCategory.Url;
        if (SelectedCategory.HasSubCategories)
        {
          if (SelectedCategory.SubCategoryDiscovered)
            ItemList.AddRange(SelectedCategory.ParentCategory.SubCategories);
          else ItemList.AddRange(SelectedCategory.LoadCategories());
        }
        else if (SelectedCategory.HasGames)
        {
          if (SelectedCategory.GamesDiscovered)
            ItemList.AddRange(SelectedCategory.Games);
          else ItemList.AddRange(SelectedCategory.LoadGames());
        }
      }
      else if (ListItem.GetType() == typeof(Game))
      {
        SelectedGame = (Game)ListItem;
        CurrentUrl = SelectedGame.Url;
        SelectedGame.LoadGame();
        ItemList.Add(SelectedGame);
      }
      return ItemList;
    }

    public virtual Image GetImageFromUrl(string imageUrl)
    {
      return Util.GetImageFromUrl(imageUrl); ;
    }
  }
}
